<template>
  <div class="article-detail__container" v-if="articleObj.art_id">
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ articleObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="articleObj.aut_name"
        :label="$timeAgo(articleObj.pubdate)">
        <template #icon>
          <img
            :src="articleObj.aut_photo"
            class="avatar">
        </template>
        <template #default>
          <div>
            <van-button
              v-if="articleObj.is_followed"
              type="info"
              size="mini"
              @click="handleFollowOrUnFollow(false)"
              >已关注</van-button>
            <van-button
              v-else
              icon="plus"
              type="info"
              size="mini"
              plain
              @click="handleFollowOrUnFollow(true)"
            >关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider>Start</van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="articleObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          v-if="articleObj.attitude === 1"
          icon="good-job"
          type="danger"
          size="small"
          @click="handleLikeorUnlike(false)"
        >已点赞</van-button>
        <van-button
          v-else
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          @click="handleLikeorUnlike(true)"
          >点赞</van-button>
      </div>
    </div>
    <!-- 文章评论 -->
    <comment-list />
  </div>
  <van-loading color="#0094ff" v-else class="loading"/>
</template>

<script>
import {
  getArticleDetailAPI,
  followAPI,
  unfollowAPI,
  likeAPI,
  unlikeAPI
} from '@/api/article'
import CommentList from './CommentList.vue'
export default {
  name: 'ArticleDetailView',
  components: { CommentList },
  data () {
    return {
      // 文章详情对象
      articleObj: {}
    }
  },
  /*
    组件缓存的目的（提高性能）：
      1. 保持组件的状态
      2. 减少无效网络请求
    注意：
      1. 组件一旦被缓存了，created()钩子只会在初始化的时候执行一次，destroyed()钩子一次都不执行
      2. 要想知道组件是被激活了（切回来了）还是失活了（切走了），借助2个专门配合组件缓存的钩子
        (1) activated:   监测组件激活
        (2) deactivated: 监测组件失活
      3. 可以给keep-alive组件添加 include 或 exclude 属性，用来匹配哪些组件会被缓存 或 哪些组件不会被缓存，
         写在include或exclude中的的字符串是组件的name属性，需要给对应的组件添加name属性

  */
  // 组件每次被激活了，都会触发一次
  // activated () {
  //   this.getArticleDetail()
  // },
  created () {
    this.getArticleDetail()
  },
  methods: {
    // 获取文章详情
    async getArticleDetail () {
      // 调接口发请求
      const resp = await getArticleDetailAPI(this.$route.query.aid)
      // 保存文章详情对象
      this.articleObj = resp.data
    },
    // 关注或取关
    async handleFollowOrUnFollow (flag) {
      try {
        // flag是true表示关注；否则取关
        // 调接口发请求
        flag
          ? await followAPI(this.articleObj.aut_id)
          : await unfollowAPI(this.articleObj.aut_id)
        // 重新获取文章详情
        await this.getArticleDetail()
        // 给个成功的提示
        flag
          ? this.$toast.success('关注成功')
          : this.$toast.success('取关成功')
      } catch (e) {
        flag
          ? this.$toast.success('关注失败')
          : this.$toast.success('取关失败')
      }
    },
    async handleLikeorUnlike (flag) {
      // flag是true表示点赞；否则是取赞
      try {
        // 调接口发请求
        flag
          ? await likeAPI(this.articleObj.art_id)
          : await unlikeAPI(this.articleObj.art_id)
        // 获取文章详情
        await this.getArticleDetail()
        // 给个成功的提示
        flag
          ? this.$toast.success('点赞成功')
          : this.$toast.success('取赞成功')
      } catch (e) {
        // 给个失败的提示
        flag
          ? this.$toast.success('点赞失败')
          : this.$toast.success('取赞失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
      width: 100%;
    }
    /deep/ pre {
      white-space: pre-wrap;
      word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

.loading {
  margin-top: 50px;
  text-align: center;
}
</style>
